<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Develop\Ctl;

use Srv\Apps\Apis\Common\IdxCtl\IdxIp;
use Srv\Apps\Proto\DTO\Develop\VerifyEmailListDTO;
use Srv\Apps\Proto\DTO\Develop\VerifySmsListDTO;
use Srv\Apps\Proto\VO\Develop\VerifyEmailListVO;
use Srv\Apps\Proto\VO\Develop\VerifyEmailVO;
use Srv\Apps\Proto\VO\Develop\VerifySmsListVO;
use Srv\Apps\Proto\VO\Develop\VerifySmsVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class VerifyRecordCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return VerifySmsListVO|null
     * getSmsVerifyList
     */
    public function getVerifySmsList(Page $Page, array $whereData): ?VerifySmsListVO
    {
        $limitString        = $Page->getLimitString();
        $whereString        = ' WHERE smvy_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case VerifySmsListDTO::_userId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'user_id='. intval($val);
                    break;
                case VerifySmsListDTO::_mobile:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'smvy_mobile LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
            }
        }
        $isFail                 = false;
        $sql                    = 'SELECT SQL_CALC_FOUND_ROWS smvy_id, smvy_code, smvy_mobile, dsmc_id, user_id, smvy_ip, smvy_send_num, smvy_expire_time, smvy_verify_code, smvy_verify, smvy_verify_time, smvy_verify_num, smvy_create_time, smvy_update_time FROM '.$this->DbAuto->getTableNameAll('sms_verify').$whereString.' ORDER BY smvy_id DESC '.$limitString;
        $verifyRecordList       = $this->DbAuto->getData($sql, $isFail);
        $totalNum               = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $VerifySmsVOList        = [];
        if(CommArray::isForeach($verifyRecordList))foreach($verifyRecordList as $verifyRecord){
            $smvyIdItem         = intval($verifyRecord['smvy_id']);
            if($smvyIdItem < 1) continue;
            $VerifySmsVO        = $this->getVerifySmsVO($verifyRecord);
            if(!($VerifySmsVO instanceof VerifySmsVO)) continue;
            $VerifySmsVOList[]  = $VerifySmsVO;
        }
        $VerifySmsListVO        = new VerifySmsListVO();
        $VerifySmsListVO->setList($VerifySmsVOList);
        $VerifySmsListVO->setPage($Page->getPageVO($totalNum));
        return $VerifySmsListVO;
    }

    /**
     * @param array $verifyRecord
     * @return VerifySmsVO|null
     * getVerifySmsVO
     */
    public function getVerifySmsVO(array $verifyRecord): ?VerifySmsVO
    {
        $smvyId             = intval($verifyRecord['smvy_id']??0);
        if($smvyId < 1) return null;
        $VerifySmsVO        = new VerifySmsVO();
        $VerifySmsVO->setSmvyId($smvyId);
        $VerifySmsVO->setCode(intval($verifyRecord['smvy_code']));
        $VerifySmsVO->setMobile($verifyRecord['smvy_mobile']);
        $VerifySmsVO->setDsmcId(intval($verifyRecord['dsmc_id']));
        $VerifySmsVO->setUserId(intval($verifyRecord['user_id']));
        $IpString           = IdxIp::tag()->getIpStrByIntStr($verifyRecord['smvy_ip']);
        $VerifySmsVO->setIpString($IpString);
        $VerifySmsVO->setSendNum(intval($verifyRecord['smvy_send_num']));
        $VerifySmsVO->setExpireTime(intval($verifyRecord['smvy_expire_time']));
        $VerifySmsVO->setVerifyCode($verifyRecord['smvy_verify_code']);
        $VerifySmsVO->setVerify(intval($verifyRecord['smvy_verify']));
        $VerifySmsVO->setVerifyTime(intval($verifyRecord['smvy_verify_time']));
        $VerifySmsVO->setVerifyNum(intval($verifyRecord['smvy_verify_num']));
        $VerifySmsVO->setCreateTime(intval($verifyRecord['smvy_create_time']));
        $VerifySmsVO->setUpdateTime(intval($verifyRecord['smvy_update_time']));
        return $VerifySmsVO;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return VerifyEmailListVO|null
     * getVerifyEmailList
     */
    public function getVerifyEmailList(Page $Page, array $whereData): ?VerifyEmailListVO
    {
        $limitString        = $Page->getLimitString();
        $whereString        = ' WHERE emvy_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case VerifyEmailListDTO::_userId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'user_id='. intval($val);
                    break;
                case VerifyEmailListDTO::_email:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'emvy_email LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
            }
        }
        $isFail                 = false;
        $sql                    = 'SELECT SQL_CALC_FOUND_ROWS emvy_id, emvy_email, demc_id, user_id, emvy_ip, emvy_send_num, emvy_expire_time, emvy_verify_code, emvy_verify, emvy_verify_time, emvy_verify_num, emvy_create_time, emvy_update_time FROM '.$this->DbAuto->getTableNameAll('email_verify').$whereString.' ORDER BY emvy_id DESC '.$limitString;
        $verifyRecordList       = $this->DbAuto->getData($sql, $isFail);
        $totalNum               = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $VerifyEmailVOList      = [];
        if(CommArray::isForeach($verifyRecordList))foreach($verifyRecordList as $verifyRecord){
            $emvyIdItem         = intval($verifyRecord['emvy_id']);
            if($emvyIdItem < 1) continue;
            $VerifyEmailVO      = $this->getVerifyEmailVO($verifyRecord);
            if(!($VerifyEmailVO instanceof VerifyEmailVO)) continue;
            $VerifyEmailVOList[]= $VerifyEmailVO;
        }
        $VerifyEmailListVO      = new VerifyEmailListVO();
        $VerifyEmailListVO->setList($VerifyEmailVOList);
        $VerifyEmailListVO->setPage($Page->getPageVO($totalNum));
        return $VerifyEmailListVO;
    }

    /**
     * @param array $verifyRecord
     * @return VerifyEmailVO|null
     * getVerifyEmailVO
     */
    public function getVerifyEmailVO(array $verifyRecord): ?VerifyEmailVO
    {
        $emvyId             = intval($verifyRecord['emvy_id']??0);
        if($emvyId < 1) return null;
        $VerifyEmailVO      = new VerifyEmailVO();
        $VerifyEmailVO->setEmvyId($emvyId);
        $VerifyEmailVO->setEmail($verifyRecord['emvy_email']);
        $VerifyEmailVO->setDemcId(intval($verifyRecord['demc_id']));
        $VerifyEmailVO->setUserId(intval($verifyRecord['user_id']));
        $IpString           = IdxIp::tag()->getIpStrByIntStr($verifyRecord['emvy_ip']);
        $VerifyEmailVO->setIpString($IpString);
        $VerifyEmailVO->setSendNum(intval($verifyRecord['emvy_send_num']));
        $VerifyEmailVO->setExpireTime(intval($verifyRecord['emvy_expire_time']));
        $VerifyEmailVO->setVerifyCode($verifyRecord['emvy_verify_code']);
        $VerifyEmailVO->setVerify(intval($verifyRecord['emvy_verify']));
        $VerifyEmailVO->setVerifyTime(intval($verifyRecord['emvy_verify_time']));
        $VerifyEmailVO->setVerifyNum(intval($verifyRecord['emvy_verify_num']));
        $VerifyEmailVO->setCreateTime(intval($verifyRecord['emvy_create_time']));
        $VerifyEmailVO->setUpdateTime(intval($verifyRecord['emvy_update_time']));
        return $VerifyEmailVO;
    }
}